﻿using System;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Win32;

namespace TwitterFollowersMonitor
{
    /// <summary>
    /// Provides methods for handling all the unexpected or non controlled exceptions in the app
    /// </summary>
    class GlobalExceptionHandler
    {

        //This private var is used to keep track about if the current Windows user session is locked or not 
        //so that the notifyIcon balloons will be shown (if user is in the session) or not (if the current session is locked or Remote desktop is disconnected)
        internal static bool SessionLocked = false;

        //This event is raised each time the current Windows user session is locked or unlocked locally or remotelly
        public static void OnSessionSwitch(Object sender, SessionSwitchEventArgs e)
        {
            switch (e.Reason)
            {
                case SessionSwitchReason.ConsoleDisconnect:
                case SessionSwitchReason.RemoteDisconnect:
                case SessionSwitchReason.SessionLock:
                    GlobalExceptionHandler.SessionLocked = true;
                    break;
                default:
                    GlobalExceptionHandler.SessionLocked = false;
                    break;
            }
        }

        /// <summary>
        /// Handles all the unexpected exceptions in the UI
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="t"></param>
        internal static void UIUnhandledException(object sender, ThreadExceptionEventArgs t)
        {
            HandleException(t.Exception);
        }

        /// <summary>
        /// Handles all the non-UI unexpected exceptions
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        internal static void NonUIUnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            HandleException( (Exception) e.ExceptionObject );
        }

        /// <summary>
        /// Shows a message with the details of the exception
        /// </summary>
        /// <param name="ex"></param>
        private static void HandleException(Exception ex)
        {
            //An error in Twitter (very common)
            if (ex is System.Net.WebException && ex.Message.IndexOf("(500)") >= 0)
            {
                MessageBox.Show("Twitter is down!!\nThis happens quite often, so never mind.\n Just try again in a few moments.", "Unexpected exception", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                //This should be done with a string resource (I'm too lazy)
                string errDetails =
    @"OPPS! Something has gone wrong!

////////////////////////////////
    {0}
////////////////////////////////

Check your Internet connection. 

If this is a recurring or important problem, please go to 

    http://twitterfollowersmon.codeplex.com/ 

and create a new Issue under the ""Issues tracker"" section indicating the text described above.

Use adescription of what you were doing when this happened and LEAVE YOUR EMAIL ADDRESS so that I can contact you for further investigation.

I'll do my best to fix it as soon as I have the time.

Regards,

    @jm_alarcon";
               
               MessageBox.Show(string.Format(errDetails, ex.Message), "Unexpected exception", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

    }
}
